package com.orangelife.mobile.myhouse.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.myhouse.activity.ResidentListActivity;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.ResidentBean2;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吴格非 on 2015/11/13.
 */
public class ResidentGvAdapter extends BaseAdapter {

    private Context mContext;
    private List<ResidentBean2> mList = new ArrayList<>();

    public ResidentGvAdapter(Context context) {
        this.mContext = context;
    }

    public void setList(List<ResidentBean2> list) {
        this.mList = list;
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gv_resident_list, null);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.ivDel = (ImageView) convertView.findViewById(R.id.ivDel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setTag(position);
        holder.ivDel.setTag(position);
        showDelIcon(holder.ivDel);
        holder.tvName.setText(mList.get(position).getName());
        holder.tvName.setOnClickListener(listener);
        holder.tvName.setOnLongClickListener(longClickListener);
        return convertView;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tvName:
                    jumpToDetail(v);
                    break;
                case R.id.ivDel:
                    showWarningDialog(v);
                    break;
            }
        }
    };

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            isShowDelIcon(true);
            return true;
        }
    };

    private void jumpToDetail(View v) {
        int pos = Integer.parseInt(String.valueOf(v.getTag()));
        String phone = mList.get(pos).getPhone();
        String identity = mList.get(pos).getType();
        String name = mList.get(pos).getName();
        HouseJumper.jumpToResidentDetail(mContext,name,phone,identity);
    }

    private void delResident(View v) {
        final int pos = Integer.parseInt(String.valueOf(v.getTag()));
        if (!StringUtil.validateList(mList, pos)) {
            return;
        }
        String applicationID = mList.get(pos).getLinkAptHouseholderID();
        ((ResidentListActivity) mContext).showDialog((Activity) mContext);
        requestDel(pos, applicationID);
    }

    private void requestDel(final int pos, String applicationID) {
        HouseHttpBiz.delResident(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                ((ResidentListActivity) mContext).closeDialog();
                boolean isOk = OrangeErrorHandler.getInstance().getInstance().isSuccess(response, "对不起，删除失败，请重试", "恭喜您，删除成功");
                if (isOk) {
                    if (!StringUtil.validateList(mList, pos)) {
                        return;
                    }
                    mList.remove(pos);
                    notifyDataSetChanged();
                }
            }
        }, applicationID);
    }

    /** 是否显示删除图标 */
    private boolean mIsShowDelIcon = false;

    private void showDelIcon(ImageView ivDel) {
        if (!mIsShowDelIcon) {
            ivDel.setVisibility(View.INVISIBLE);
            ivDel.setOnClickListener(null);
        } else {
            ivDel.setVisibility(View.VISIBLE);
            ivDel.setOnClickListener(listener);
        }
    }

    public void isShowDelIcon(boolean isShowDelIcon) {
        mIsShowDelIcon = isShowDelIcon;
        notifyDataSetChanged();
    }

    private void showWarningDialog(final View v) {
        final ConfirmDialog dialog = new ConfirmDialog(mContext, "提示", "确定要删除吗",
                "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                delResident(v);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    class ViewHolder {
        TextView tvName;
        ImageView ivDel;
    }
}
